<?php
session_start();
require_once('./dbconnect.class.php');    //导入class文件
require_once('./db_config.php');
header('Content-Type:text/html;charset=utf-8');

$dbconnect = new dbconnect($localhost,$username,$password,$dbname);



for ($k=0; $k < 100; $k++) { 
	$data = $dbconnect->selectOne('follow',array('is_check'=>'0'));
	$fid = $data['fid'];
	$result = curl_get($fid);
	$pages = $result['pages'];
	foreach ($result['list'] as $key => $value) {
		if (!$dbconnect->select('follow',array('fid'=>$value['fid']))) {
			$insert = array(
					'fid'=>$value['fid'],
					'addtime'=>$value['addtime'],
					'record_id'=>$value['record_id'],
					'uname'=>$value['uname'],
					'face'=>$value['face'],
					'is_check'=>0
				);
			$dbconnect->insert('follow',$insert);
		}
	}
	for($i=2;$i<=$pages;$i++){
		$result = curl_get($fid,$i);
		if (!empty($result['list'])) {
			foreach ($result['list'] as $key => $value) {
				if (!$dbconnect->select('follow',array('fid'=>$value['fid']))) {
					$insert = array(
							'fid'=>$value['fid'],
							'addtime'=>$value['addtime'],
							'record_id'=>$value['record_id'],
							'uname'=>$value['uname'],
							'face'=>$value['face'],
							'is_check'=>0
						);
					$dbconnect->insert('follow',$insert);
				}
			}
		}
	}
	$dbconnect->update('follow',array('is_check'=>'1'),array('fid'=>$fid));
	echo $k;
	sleep(3);
}



function curl_get($fid,$page=1)
{
	$url = "https://space.bilibili.com/ajax/friend/GetAttentionList?mid=".$fid."&page=".$page;
	// var_dump($url);die;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_HEADER, false);
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_REFERER, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
	$result = curl_exec($ch);
	curl_close($ch);
	return json_decode($result,true)['data'];
}

?>


